<html>
<head>
<script src="../../resources/js-test.js"></script>
<style>
@font-face {
    font-family: Font1;
    src: local(Arial);
}

@font-face {
    font-family: Font2;
    src: local(Arial);
}

@font-face {
    font-family: Font3;
    src: local(Arial);
}
</style>
<script>
description('Tests Set operations of FontFaceSet.');

thisArg = {};
faces = []

function checkResults() {
    shouldBe('faces.length', '3');

    shouldBeEqualToString('faces[0].family', 'Font1');
    shouldBeEqualToString('faces[1].family', 'Font2');
    shouldBeEqualToString('faces[2].family', 'Font3');

    shouldBeTrue('document.fonts.has(faces[0])');
    shouldBeTrue('document.fonts.has(faces[1])');
    shouldBeTrue('document.fonts.has(faces[2])');
}

function callback(face, faceAgain, set) {
    if (faces.length == 0) {
        callbackArgs = arguments;
        thisValue = this;
        shouldBeType('callbackArgs[0]', 'FontFace');
        shouldBeTrue('callbackArgs[0] === callbackArgs[1]');
        shouldBeTrue('callbackArgs[2] === document.fonts');
        shouldBeTrue('thisValue === thisArg');
    }
    faces.push(face);
}

function runTests() {
    nonCssConnectedFace = new FontFace("MyFont", "local(Arial)", {});

    shouldBe('document.fonts.size', '3');

    debug('check forEach');
    document.fonts.forEach(callback, thisArg);
    checkResults();

    debug('check keys');
    faces = [];
    for (face of document.fonts.keys())
        faces.push(face);
    checkResults();

    debug('check values');
    faces = [];
    for (face of document.fonts.values())
        faces.push(face);
    checkResults();

    debug('check entries');
    faces = [];
    for (entry of document.fonts.entries()) {
        shouldBeTrue('entry[0] === entry[1]');
        faces.push(entry[1]);
    }
    checkResults();

    shouldThrow('document.fonts.has(null)');
    shouldThrow('document.fonts.has("Font1")');
    shouldThrow('document.fonts.add(null)');
    shouldThrow('document.fonts.add("Font1")');
    shouldThrow('document.fonts.delete(null)');
    shouldThrow('document.fonts.delete("Font1")');

    shouldBeFalse('document.fonts.has(nonCssConnectedFace)');

    shouldNotThrow('document.fonts.add(faces[0])');
    shouldBe('document.fonts.size', '3');

    shouldBeFalse('document.fonts.delete(faces[0])');
    shouldBe('document.fonts.size', '3');

    document.fonts.add(nonCssConnectedFace);
    shouldBe('document.fonts.size', '4');
    shouldBeTrue('document.fonts.has(nonCssConnectedFace)');
    document.fonts.add(nonCssConnectedFace);
    shouldBe('document.fonts.size', '4');
    document.fonts.delete(nonCssConnectedFace);
    shouldBe('document.fonts.size', '3');
    shouldBeFalse('document.fonts.has(nonCssConnectedFace)');
    document.fonts.delete(nonCssConnectedFace);
    shouldBe('document.fonts.size', '3');

    document.fonts.add(nonCssConnectedFace);
    shouldBe('document.fonts.size', '4');
    document.fonts.clear();
    shouldBe('document.fonts.size', '3');
}

if (document.fonts)
    runTests();
else
    testFailed('document.fonts does not exist');

</script>
</head>
<body>
</body>
</html>
